<?php
/**
 * report.php 数据服务类
 */

class UserService extends Service {
    public $table;
    public $userLimit = 100000;
    private $codestrs = array("a2mx1k3l4r5h6s7d8p9ifc","o7x4i3r2u1n8y9mpdwas","a3s49df2qwerfgjh","f8c91dlu7q8e","p8i6o5u2ljhdf","4u3y2x98ndk6edsabv","za3s49df2qwerfgjh","f8c91delu7q8e","xp8i6o5u2ljhdf");

	function __construct() {
		parent::__construct();
        $this->table = 'user_main';
	}

	// code 格式 nxxxxxx n 代表使用的第几个密文
	function code2n($code='', $xornum=20170718) {
		if(strlen($code)<2) return false;
		$randkey = substr($code,-1,1);
		$code = substr($code,0, -1);
		$str = $this->codestrs[$randkey];
		$len = strlen($code);
		$rst = '';
		while($len--) {
			$rst .= strpos($str, substr($code, $len, 1));
		}
		$n = intval($rst);
		return $n^$xornum;
	}

	function n2code($n, $xornum=20170718) {
		$n = intval($n);
		$n = $n?$n:0;
		$n = $n^$xornum;
		$n = $n.'';
		$randkey = rand(0, count($this->codestrs)-1);
		$str = $this->codestrs[$randkey];
		$len = strlen($n);
		$code = '';
		while($len--) {
			$code .= $str[substr($n, $len, 1)];
		}
		return $code.$randkey;
	}

	// 是否有领取记录
	function isLogedByOpenid($openid) {
		if(!$openid) return false;
		$sql = "select * from `{$this->table}` where openid='{$openid}'" ;
		$_list = $this->db->executeSQL($sql);
		return is_array($_list)?true:false;
	}

	// 主键获取信息
	function getById($id) {
		if(!$id) return false;
		$sql = "select * from `{$this->table}` where id='{$id}' limit 0,1" ;
		$_list = $this->db->executeSQL($sql);
		return @$_list[0];
	}

	// 获取信息
	function getByOpenid($openid) {
		if(!$openid) return false;
		$sql = "select * from `{$this->table}` where openid='{$openid}' limit 0,1" ;
		$_list = $this->db->executeSQL($sql);
		return @$_list[0];
	}

	// 获取信息
	function getByMobile($mobile) {
		if(!$mobile) return false;
		$sql = "select * from `{$this->table}` where mobile='{$mobile}' limit 0,1" ;
		$_list = $this->db->executeSQL($sql);
		return @$_list[0];
	}

	// 添加记录
	function addItem($data=array()) {
		$data = UtilHelper::safe_sql($data);
		$openid = @$data['openid'];
		$nickname = @$data['nickname'];
		$mobile = @$data['mobile'];

		if(!$openid) {return false;}

		$addtime = VAR_TIME_NOW;
		$uptime = VAR_TIME_NOW;
        $regip = getClientIp();
        $lastip = getClientIp();

		$user = $this->getByOpenid($openid);
        if(!empty($user)) {
            $sql = "update `{$this->table}` set `lastip`='{$lastip}',`uptime`={$uptime} where `openid`='{$openid}'";
        } else {
            $sql = "insert into `{$this->table}` (`openid`,`nickname`,`mobile`,`regid`,`lastip`, `addtime`,`uptime`) values ('{$openid}', '{$nickname}', '{$mobile}', '{$regip}', '{$lastip}', {$addtime})";
        };
		$this->db->executeSQL($sql);
		return $this->db->lastInsertID();
	}

	// 获取用户数量
	function count() {
		$sql = "select count(id) num from `{$this->table}`";
		$rst = $this->db->executeSQL($sql);
		return $rst[0]['num'];
	}

}
?>

